package org.smoozik.app.common.partition;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JDesktopPane;

import org.smoozik.app.common.partition.component.UIMusicComponent;
import org.smoozik.app.common.partition.component.UISong;
import org.smoozik.app.ihm.Constants;
import org.smoozik.services.musictheory.Figure;
import org.smoozik.services.sequence.Song;

@SuppressWarnings("serial")
public class SongPanel extends JDesktopPane {

	int height;
	int margin;
	int length;
	int interval;

	private Song song;
	private UISong uiSequence;

	PartitionClient partitionClient;
	int time;
	Color tickColor;

	UISong uiSong;

	// Constructors

	public SongPanel() {
		super();
		height = Constants.DEFAULT_HEIGHT;
		margin = Constants.DEFAULT_MARGIN;
		length = Constants.DEFAULT_LENGTH;
		interval = Constants.DEFAULT_INTERVAL;

		setDefaultListener();
		time = 0;
	}

	// Getters setters

	public Song getSong() {
		return song;
	}

	public UISong getUiSong() {
		return uiSong;
	}

	public void setUiSong(UISong uiSong) {
		this.uiSong = uiSong;
	}

	public UISong getUiSequence() {
		return uiSequence;
	}

	public int getTime() {
		return time;
	}

	public void setTime(int time) {
		this.time = time;
	}

	public Color getTickColor() {
		return tickColor;
	}

	public void setTickColor(Color tickColor) {
		this.tickColor = tickColor;
	}

	public PartitionClient getPartitionClient() {
		return partitionClient;
	}

	public void setPartitionClient(PartitionClient partitionClient) {
		this.partitionClient = partitionClient;
	}

	// Methods

	public ArrayList<UIMusicComponent> getMusicComponents() {
		if (uiSong != null) {
			return uiSong.getMusicComponents();
		} else {
			return null;
		}
	}

	public void setSong(Song song) {
		this.song = song;

		uiSong = new UISong(song);
		
	}

	public int getTotalLength() {
		return this.margin * 2 + this.length;
	}

	public Figure getFigureForMouse(MouseEvent e) {
		int x = e.getX();
		int y = e.getY();
		// System.out.println("x = " + x);
		Figure figure = getFigureForPoint(x, y);

		return figure;
	}

	public void updateKeyPressed(Figure figure) {
		if (figure != null) {
			this.partitionClient.keyPressed(figure);
		}
	}

	private Figure getFigureForPoint(int x, int y) {
		Figure figureLit = null;

		// chords printing
		if (song != null) {
			for (UIMusicComponent component : uiSong.getMusicComponents()) {
				if (component != null) {
					if (component.contains(x, y)) {
						figureLit = component.getFigure();
						break;
					}

				}
			}
		}

		return figureLit;
	}

	public void setDefaultListener() {
		SongMouseListener pl = new SongMouseListener(this);
		this.addMouseListener(pl);
	}

	public void removeListener() {
		MouseListener[] listeners = this.getMouseListeners();
		if (listeners != null) {
			for (int i = 0; i < listeners.length; i++) {
				this.removeMouseListener(listeners[i]);
			}
		}
	}

	// public void resetFigureMargin() {
	// this.figureMargin = 0;
	// }
	public void paintComponent(Graphics g) {
//		super.paintComponent(g);
		// if (!painted) {

		// g.setColor(Color.WHITE);
		// g.fillRect(0, 0, 2000, 2000);
		// g.setColor(Color.BLACK);
		// g.drawImage(Constants.backgroundImg, 0, 0, null);
		
		
		g.setColor(tickColor);
		String s = time + "";
		g.drawChars(s.toCharArray(), 0, s.length(), 20, 20);

		// rule display
		// g.setColor(Color.RED);
		// g.drawLine(1, 1, 1, 1000);
		// for (int i = 0; i < 200; i++) {
		// g.drawLine(1, i * 40, 20, i * 40);
		// }

		g.setColor(Color.BLACK);
		if (song != null) {
			String speed = "speed : " + song.getTimeWeight();
			g.drawChars(speed.toCharArray(), 0, speed.length(), 50, 20);

			// song painting
			if (uiSong != null) {
				// for (UISong seq : song.getUiSequences()) {
				uiSong.paint(g, getVisibleRect());
				// }
			}
		}
	}

}
